<template>
  <el-dialog title="项目选择列表" :visible="visible" width="700px" @close="$emit('close')">
    <!-- 项目名模糊查询区域 -->
    <div style="margin-bottom: 15px; display: flex; align-items: center;">
      <!-- 添加外层 el-form -->
      <el-form :model="queryForm" :inline="true" style="display: flex; align-items: center; margin-bottom: 0;">
        <el-form-item label="项目名称" style="margin-bottom: 0; margin-right: 10px;">
          <el-input v-model="queryForm.pname" placeholder="请输入" @input="getProjectList"></el-input>
        </el-form-item>
        <el-form-item style="margin-bottom: 0;">
          <el-button type="primary" @click="getProjectList">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    
    <el-table  
      :data="projectList"  
      @row-click="handleRowClick" 
      :row-class-name="handleRowClassName"  
      border 
      style="width: 100%; margin-bottom: 15px;" 
    >
      <el-table-column prop="serialNum" label="序号" width="80" align="center"></el-table-column>
      <el-table-column prop="pname" label="项目名称" align="center"></el-table-column>
      <el-table-column prop="dname" label="负责部门" align="center"></el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryForm.page"
      :page-sizes="[5, 7, 10, 15]"
      :page-size="queryForm.limit"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>

    <div slot="footer">
      <el-button @click="$emit('close')">取消</el-button>
      <el-button type="primary" @click="handleConfirm">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'ProjectSelect',
  props: {
    visible: Boolean,
    deptno: Number, 
    dname:String
  },
  data() {
    return {
      projectList: [], 
      selectedProject: null, 
      queryForm: {
        pname: '',
        deptno: this.deptno,
        page: 1,
        limit: 7,
      },
      total: 0, 
    };
  },
  created() {
    this.getProjectList(); 
  },
  methods: {
    getProjectList() {
      this.$http.post('/getProjects', this.queryForm).then((res) => {
        this.projectList = res.data.data || [];
        this.projectList.forEach((item, index) => {
          item.serialNum = (this.queryForm.page - 1) * this.queryForm.limit + index + 1;
        });
        this.total = res.data.total || 0;
      });
    },
    handleRowClick(row) {
      this.selectedProject = row;
    },
    handleRowClassName({ row }) {
      return row === this.selectedProject ? 'selected-row' : '';
    },
    handleSizeChange(val) {
      this.queryForm.limit = val;
      this.queryForm.page = 1;
      this.getProjectList();
    },
    handleCurrentChange(val) {
      this.queryForm.page = val;
      this.getProjectList();
    },
    handleConfirm() {
      this.$emit('confirm', this.selectedProject);
      this.$emit('close');
    },
  },
};
</script>

<style scoped>
::v-deep .selected-row {
  background-color: #f0f9eb !important; /* 可自定义选中行背景色 */
}

/* 确保表单项垂直对齐 */
::v-deep .el-form-item {
  display: flex;
  align-items: center;
}
</style>